package com.cssw.fyzb.busi.service.exchange;

import com.cssw.fyzb.core.dao.user.UserDao;
import com.cssw.fyzb.core.model.vo.acct.AcctGlobalVo;
import com.cssw.fyzb.busi.service.acct.AcctService;
import com.cssw.fyzb.busi.service.acct.AcctServiceImpl;
import com.cssw.fyzb.core.dao.exchange.YExchangeFDefDao;
import com.cssw.fyzb.core.dao.exchange.YExchangeFLogDao;
import com.cssw.fyzb.core.dao.system.SysParamDao;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.core.util.AesUtil;
import com.cssw.fyzb.core.util.BizUtil;
import com.cssw.fyzb.core.util.StringUtil;
import com.ydn.dbframe.plugin.activerecord.Db;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

@Repository
public class YExchangeFServiceImpl implements YExchangeFService {
    private static final Logger logger = LoggerFactory.getLogger(AcctServiceImpl.class);
    private static final int DEF_DIV_SCALE = 7;
    @Autowired
    private YExchangeFLogDao yExchangeFLogDao;
    @Autowired
    private AcctService acctService;
    @Autowired
    private YExchangeFDefDao yExchangeFDefDao;
    @Autowired
    private SysParamDao sysParamDao;

    @Autowired
    private UserDao userDao;

    @Override
    public Boolean yExchangeFDef(BigDecimal num, Long userId, Long yfId, int type, String pwd) {

        User user = userDao.getById(userId);
        if (StringUtil.isEmpty(user.getTradePwd())) {
            BizUtil.exception("您还未设置交易密码");
        }
        if (StringUtil.isEmpty(pwd)) {
            BizUtil.exception("请输入密码");
        }
        if (!AesUtil.encode(pwd).equals(user.getTradePwd())) {
            BizUtil.exception("交易密码错误");
        }
        boolean flag = Db.tx(() -> {

            BigDecimal yNum = null;
            BigDecimal fValue = null;

            AcctGlobalVo acctGlobalVo = acctService.queryAcctGlobal(userId);
            SysParam sysParam = sysParamDao.findByCode("Y_EXCHANGE_F_RATIO");
            if (sysParam.getParamValue() == null) BizUtil.exception("参数值有误请联系管理员，请确认后在操作");

            //全部兑换
            if (type == 1) {
                if (acctGlobalVo.getYBalance() == null) BizUtil.exception("您的音值为空，请确认后在操作");
                yNum = acctGlobalVo.getYBalance();
                fValue = yNum.multiply(new BigDecimal(sysParam.getParamValue()));
            } else if (type == 2) {
                YExchangeFDef yExchangeFDef = yExchangeFDefDao.getYExchangeFDef(yfId);
                if (yExchangeFDef.getYCoin() == null) BizUtil.exception("您的音值为空，请确认后在操作");
                if (yExchangeFDef.getYCoin().compareTo(acctGlobalVo.getYBalance()) >= 1) BizUtil.exception("您的兑换金额有误，请确认后在操作");
                yNum = yExchangeFDef.getYCoin();
                fValue = yExchangeFDef.getFCoin();
            } else {
                if (num.compareTo(acctGlobalVo.getYBalance()) >= 1) BizUtil.exception("您的兑换金额有误，请确认后在操作");
                yNum = num;
                fValue = num.multiply(new BigDecimal(sysParam.getParamValue()));
            }

            Long id = yExchangeFLogDao.updateData(userId, yNum, fValue);

            acctService.addBalance(userId, "F", fValue, id.toString(),
                    AcctTradeType.EXCHANGE_ADD, BusiType.EXCHANGE_CODE, "音值兑换福币");

            acctService.minusBalance(userId, "Y", yNum, id.toString(),
                    AcctTradeType.EXCHANGE_REDUCE, BusiType.EXCHANGE_CODE, "音值兑换福币");
//            //音值福币
//            acctService.updateCmfUser(userId, fValue.intValue(), yNum.intValue());
//            //sum 最后的音值
//            acctLogEao.save(UuidUtil.get(), userId, userId, AcctType.ACCT_TYPE_Y, yNum,
//                    Acct.FEE_IN_DE_MINUS, userId.toString(), AcctTradeType.SETTLEMENT_REDUCE_Y, AcctTradeType.SETTLEMENT_REDUCE_NAME_Y,
//                    BusiType.Y_EXCHANDE_F_Y, new Date());
//            //福币
//            acctLogEao.save(UuidUtil.get(), userId, userId, AcctType.ACCT_TYPE_F, fValue,
//                    Acct.FEE_IN_DE_ADD, userId.toString(), AcctTradeType.SETTLEMENT_REDUCE_F, AcctTradeType.SETTLEMENT_REDUCE_NAME_F,
//                    BusiType.Y_EXCHANDE_F_F, new Date());
            return true;
        });
        return flag;
    }
}
